package com.zhongshengnetwork.rightbe.dbservice;

import android.util.Log;

import com.zhongshengnetwork.rightbe.Constant.LetterConstant;
import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.dbmodel.LetterdbModel;

import org.xutils.DbManager;
import org.xutils.db.sqlite.WhereBuilder;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.List;

/**
 * Created by luojingxing on 2019/8/3.
 */

public class LetterdbService {

    //插入或者更新数据库
    public static boolean saveOrUpdate(LetterdbModel model){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            db.saveOrUpdate(model);
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    //获取总的未读消息数量
    public static long getAllUnReadCount(String userid){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            if (db.selector(LetterdbModel.class).where("userid","==",userid).and("isRead","==",0) != null){
                return db.selector(LetterdbModel.class).where("userid","==",userid).and("isRead","==",0).count();
            }
        } catch (DbException e) {
            e.printStackTrace();
            return 0;
        }
        return 0;
    }

    //获取私聊的未读消息数量
    public static int getChatUnReadCount(String userid, String fromuserid){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            if (db.selector(LetterdbModel.class).where("userid","==",userid).and("fromuserid","==",fromuserid).and("isFirst","==",0).and("isRead","==",0) != null)
                return (int)db.selector(LetterdbModel.class).where("userid","==",userid).and("fromuserid","==",fromuserid).and("isFirst","==",0).and("isRead","==",0).count();
        } catch (DbException e) {
            e.printStackTrace();
            return 0;
        }
        return 0;
    }

    //获取会话列表
    public static List<LetterdbModel> getConversation(String userid, int pageIndex, int pageRecord){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            return db.selector(LetterdbModel.class).where("userid","==",userid).and("isFirst","==",1).orderBy("time", true).offset(pageIndex).limit(pageRecord).findAll();
        }catch (DbException e){
            e.printStackTrace();
        }
        return null;
    }

    //获取私聊消息
    public static List<LetterdbModel> getChat(String userid, String fromuserid, int pageIndex, int pageRecord){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            return db.selector(LetterdbModel.class).where("userid","==",userid).and("fromuserid","==",fromuserid).and("isFirst","==",0).orderBy("time", true).offset(pageIndex).limit(pageRecord).findAll();
        }catch (DbException e){
            e.printStackTrace();
        }
        return null;
    }

    //获取单条消息
    public static LetterdbModel getByMsgId(String msgId){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            LetterdbModel model = db.findById(LetterdbModel.class,msgId);
            return model;
        } catch (DbException e) {
            e.printStackTrace();
            return null;
        }
    }

    //根据ID获取单条消息
    public static LetterdbModel getById(int id){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            return db.selector(LetterdbModel.class).where("id","==",id).and("isFirst","==",0).orderBy("time", true).findFirst();
        }catch (DbException e){
            e.printStackTrace();
        }
        return null;
    }

    //更新为已读
    public static boolean changeToReadState(String userid, String fromuserid){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            if (db.selector(LetterdbModel.class).where("userid","==",userid).and("fromuserid","==",fromuserid).and("isRead","==",0) != null){
                List<LetterdbModel> list = db.selector(LetterdbModel.class).where("userid","==",userid).and("fromuserid","==",fromuserid).and("isRead","==",0).findAll();
                if (list != null && list.size() > 0){
                    for (LetterdbModel model : list){
                        model.setRead(true);
                        db.saveOrUpdate(model);
                    }
                }
            }
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    //标志为发布失败
    public static boolean changeToSendFailState(String userid, String fromuserid){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            if (db.selector(LetterdbModel.class).where("userid","==",userid).and("fromuserid","==",fromuserid).and("state","==",1) != null){
                List<LetterdbModel> list = db.selector(LetterdbModel.class).where("userid","==",userid).and("fromuserid","==",fromuserid).and("state","==",1).findAll();
                if (list != null && list.size() > 0){
                    for (LetterdbModel model : list){
                        model.setState(LetterConstant.sx_sendFail_state);
                        db.saveOrUpdate(model);
                    }
                }
            }
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    //删除全部私聊
    public static boolean deleteChat(String userid, String fromuserid){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            db.delete(LetterdbModel.class, WhereBuilder.b("userid", "==", userid).and("fromuserid","==",fromuserid));
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    //删除单条数据
    public static boolean deleteByMsgId(String msgId){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            db.deleteById(LetterdbModel.class, msgId);
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

    //更新聊天列表
    public static boolean updateConversation(LetterdbModel model, boolean isDelete){
        DbManager db = x.getDb(CustomApplication.daoConfig);
        try {
            if (isDelete == true){
                db.deleteById(LetterdbModel.class, model.getUserid()+model.getFromuserid());
            }else {
                model.setUnReadCount(LetterdbService.getChatUnReadCount(model.getUserid(),model.getFromuserid()));
                model.setMsgId(model.getUserid()+model.getFromuserid());
                Log.e("TAG","保存的msgId="+model.getMsgId());
                model.setIsFirst(1);
                model.setRead(true);
                db.saveOrUpdate(model);
            }
            return true;
        } catch (DbException e) {
            e.printStackTrace();
            return false;
        }
    }

}
